import { createRouter, createWebHistory } from 'vue-router'
import { isAuthenticated } from '@/utils/auth'
import { showToast } from 'vant'

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Index.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { requiresAuth: false }
  },
  // 用户相关路由
  {
    path: '/user',
    name: 'UserCenter',
    component: () => import('@/views/user/UserCenter.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/profile',
    name: 'Profile',
    component: () => import('@/views/user/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/profile/edit',
    name: 'ProfileEdit',
    component: () => import('@/views/user/ProfileEdit.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/favorites',
    name: 'Favorites',
    component: () => import('@/views/user/Favorites.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/activity',
    name: 'UserActivity',
    component: () => import('@/views/user/UserActivity.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/browse-history',
    name: 'BrowseHistory',
    component: () => import('@/views/user/BrowseHistory.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/diet-record',
    name: 'DietRecord',
    component: () => import('@/views/user/DietRecord.vue'),
    meta: { requiresAuth: true }
  },
  // 食谱相关路由
  {
    path: '/recipe/list',
    name: 'RecipeList',
    component: () => import('@/views/recipe/List.vue')
  },
  {
    path: '/recipe/search',
    name: 'RecipeSearch',
    component: () => import('@/views/recipe/Search.vue')
  },
  {
    path: '/recipe/new',
    name: 'NewRecipes',
    component: () => import('@/views/recipe/New.vue')
  },
  {
    path: '/recipe/analysis',
    name: 'RecipeAnalysis',
    component: () => import('@/views/recipe/Analysis.vue')
  },
  {
    path: '/recipe/:id',
    name: 'RecipeDetail',
    component: () => import('@/views/recipe/Detail.vue')
  },
  // 食材相关路由
  {
    path: '/food/list',
    name: 'FoodList',
    component: () => import('@/views/food/List.vue')
  },
  {
    path: '/food/:id',
    name: 'FoodDetail',
    component: () => import('@/views/food/Detail.vue')
  },
  // 个性化推荐
  {
    path: '/personalized',
    name: 'PersonalizedRecommendation',
    component: () => import('@/views/recommendation/Personalized.vue'),
    meta: { requiresAuth: true }
  },
  // 热门推荐
  {
    path: '/recommendation',
    name: 'Recommendation',
    component: () => import('@/views/recommendation/Popular.vue')
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(''),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 需要登录的页面
  if (to.meta.requiresAuth && !isAuthenticated()) {
    showToast('请先登录')
    next('/login')
  } else {
    next()
  }
})

// 导出router实例，而不是在这里初始化auth
export default router 